//
//  KDChildInstructController.m
//  WangJingApp
//
//  Created by 魏强 on 17/2/28.
//  Copyright © 2017年 KDTech. All rights reserved.
//

#import "KDChildInstructController.h"
#import "SVProgressHUD.h"
#import "KDNotyRequestTool.h"
#import "KDInstructCell.h"
#import "UITableView+FDTemplateLayoutCell.h"
#import "KDDetailInstructController.h"
@interface KDChildInstructController ()
@property (nonatomic, strong) NSMutableArray *infoArr;
@property (nonatomic, copy) NSString * cellId;

@property (nonatomic, copy) NSString * page;

@end

@implementation KDChildInstructController
#pragma mark - 懒加载区
- (NSMutableArray *)infoArr
{
    if (!_infoArr) {
        _infoArr = [NSMutableArray array] ;
    }
    return _infoArr;
}
#pragma mark - 生命周期区
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupTableView];
}
#pragma mark - 板块布置区
- (void)setupTableView{
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 64, 0);
    self.cellId = @"KDInstructCell";
    UINib *xib = [UINib nibWithNibName:self.cellId bundle:nil];
//    self.tableView.tag = self.type.integerValue;
    
    
    [self.tableView registerNib:xib forCellReuseIdentifier:self.cellId];
    self.tableView.separatorColor = WQColor(245, 245, 245);
    
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    [footer setTitle:@"" forState:MJRefreshStateIdle];
    // 设置footer
    self.tableView.footer = footer;
    [self.tableView.header beginRefreshing];
}
#pragma mark - 事件点击区

#pragma mark - 网络请求区
- (void)loadData{
    self.page = @"1";
    KDNotyBoxParam *param = [[KDNotyBoxParam alloc]init];
    param.method = (self.type.integerValue)?@"message/outBox":@"message/receivedMsg";
    //    param.isSignIn = self.type;
    param.pageNo = self.page;
    param.pageSize = @"10";
    [KDNotyRequestTool filterMsgWithParam:param success:^(KDNotyBoxResult *responseObejct) {
        if (responseObejct.status != 1) {
            [SVProgressHUD showErrorWithStatus:@"请求错误" maskType:SVProgressHUDMaskTypeGradient];
        }else{
            NSDictionary *dic = responseObejct.data;
            NSArray *arr = dic[@"list"];
            self.infoArr = [KDInstructCellModel objectArrayWithKeyValuesArray:arr];
        }
        [self.tableView.header endRefreshing];
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"请求错误" maskType:SVProgressHUDMaskTypeGradient];
        [self.tableView.header endRefreshing];
    }];
}
- (void)loadMoreData{
    self.page = [NSString stringWithFormat:@"%ld",(long)self.page.integerValue + 1] ;
    KDNotyBoxParam *param = [[KDNotyBoxParam alloc]init];
    param.method = (self.type.integerValue)?@"message/outBox":@"message/receivedMsg";
    //    param.isSignIn = self.type;
    param.pageNo = self.page;
    param.pageSize = @"10";
    [KDNotyRequestTool filterMsgWithParam:param success:^(KDNotyBoxResult *responseObejct) {
        if (responseObejct.status != 1) {
            [SVProgressHUD showErrorWithStatus:@"请求错误" maskType:SVProgressHUDMaskTypeGradient];
            [self.tableView.footer endRefreshing];
        }else{
            NSDictionary *dic = responseObejct.data;
            NSArray *arr = dic[@"list"];
            if (arr.count) {
                NSMutableArray *muteArr = [NSMutableArray arrayWithArray:self.infoArr];
                arr = [KDInstructCellModel objectArrayWithKeyValuesArray:arr];
                [muteArr addObjectsFromArray:arr];
                self.infoArr = muteArr;
                [self.tableView.footer endRefreshing];
                [self.tableView reloadData];
                
            }else{//
                [self.tableView.footer noticeNoMoreData];
            }
            
            
        }
    } failure:^(NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"请求错误" maskType:SVProgressHUDMaskTypeGradient];
        [self.tableView.footer endRefreshing];
    }];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.infoArr.count;
}

#pragma mark - Table view data source
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    KDInstructCell *cell = [KDInstructCell cellWithTableView:tableView];
//    cell.delegate = self;
    if (self.infoArr.count != 0) {
        cell.model = self.infoArr[indexPath.row];
    }
    return cell;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [tableView fd_heightForCellWithIdentifier:self.cellId configuration:^(KDInstructCell * cell) {
        cell.model = self.infoArr[indexPath.row];
    }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    KDDetailInstructController *detail = [[KDDetailInstructController alloc]init];
    [self.navigationController pushViewController:detail animated:YES];
}
#pragma mark - 通知接收区


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
